package com.ss.common.taglib;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.ss.common.pojo.PageResult;
 
/**
 * @author lilongshun
 * @version 1.0.0
 * @date  Oct 15, 2010 10:33:02 AM
 * 分页标签 V2.0
 * 
 */
public class TagPage extends TagSupport {
	Logger log = Logger.getLogger(TagPage.class);
	// 接收表单别名
	private String formName = null;
	private String pageSize =null;
	private int maxSize=200;
	private String validate;
	
	//常量
	
	public String getValidate() {
		return validate;
	}

	public void setValidate(String validate) {
		this.validate = validate;
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	@Override
	public int doEndTag() throws JspException {
		PageResult pageResult = null;
		// 尝试从request中取出表单Bean
		pageResult = (PageResult) pageContext.getRequest().getAttribute(formName);
		if (pageResult == null) {
			pageResult = (PageResult)pageContext.getRequest().getAttribute(formName);
		}
		if (pageResult != null) {
			if(!pageResult.getIsPaging()){
				return EVAL_PAGE;
			}
			String temp_readonly="";
			if(pageSize!=null){				
				pageResult.setPageSize(Integer.parseInt(pageSize));			
				temp_readonly="readonly";
			}
			StringBuffer buffer = new StringBuffer();
			HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
			//
			buffer.append("<input type='hidden' name='submitType' id='submitType'><div align='right' style='margin:3px 3px 3px 3px'>&nbsp;&nbsp;").append(pageResult.getCurrent()+"/"+pageResult.getTotalPage()).append("页&nbsp;");
			
			buffer.append("&nbsp;共").append(pageResult.getTotalRecord()).append("条&nbsp;");
			buffer.append("&nbsp;&nbsp;");
			
			if (pageResult.isPre()) {
				// 有上一页
				buffer.append("<input type='button' class='first' onclick='javascript:doSubmit(1);' title='首页'>&nbsp;&nbsp;");
				buffer.append("<input type='button'  class='prev' onclick='javascript:doSubmit("+ (pageResult.getCurrent() - 1) + ")' title='上一页'>&nbsp;&nbsp;");
			}else{
				buffer.append("<input type='button' class='first_d' disabled title='首页'>&nbsp;&nbsp;");
				buffer.append("<input type='button'  class='prev_d' disabled title='上一页'>&nbsp;&nbsp;");
			}
			if (pageResult.getCurrent() < pageResult.getTotalPage()) {
				// 有下一页
				buffer.append("<input type='button'  class='next' title='下一页' onclick='javascript:doSubmit("+ (pageResult.getCurrent() + 1) + ")'>&nbsp;&nbsp;");
				buffer.append("<input type='button'  class='last' title='末页' onclick='javascript:doSubmit("+ pageResult.getTotalPage() + ")'>&nbsp;&nbsp;");
			}else{
				buffer.append("<input type='button'  class='next_d' title='下一页' disabled>&nbsp;&nbsp;");
				buffer.append("<input type='button'  class='last_d' title='末页' disabled>&nbsp;&nbsp;");
			}
			buffer.append("&nbsp;&nbsp;");
			buffer.append("第").append("<select name='current' id='current' onchange='doSubmit(this.value)'>");			
			for (int i = 1; i <= pageResult.getTotalPage(); i++) {
				buffer.append("<option value='" + i + "'");
				if (i == pageResult.getCurrent()) {
					buffer.append(" selected");
				}
				buffer.append(">" + i + "</option>");
			}
			buffer.append("</select>").append("页");
			buffer.append("&nbsp;&nbsp;");
			buffer.append("每页").append(
					"<input "+temp_readonly+" title='max size "+maxSize+"' type='text'  name='pageSize' id='pageSize' onchange='checkPageSize(this)'  maxSize='"+maxSize+"' size='1' value='"
							+ pageResult.getPageSize() + "' />").append("条");
			buffer.append("<input value='' class='page_find' type='button' onclick=\"doSubmit($('#current').val())\" title='search' /></div>");
									
			buffer.append("<script type='text/javascript'>").append(
					"function doSubmit(pageNum){var la=document.getElementById('pageSize');var lx=la.value;document.forms[0].reset();la.value=lx;");
			
			buffer.append("document.getElementById('current').value=pageNum;")
					.append("document.getElementById('submitType').value='byControl';document.forms[0].submit();}</script>");
			try {
					pageContext.getOut().print(buffer.toString()); 
					buffer.delete(0, buffer.length());
					pageResult=null;					
				} catch (Exception e) {
					log.error("分页控件出现异常"+e.getMessage());
				}
		}else{
			StringBuffer buffer = new StringBuffer();
			buffer.append("<input type='hidden' name='current' id='current' value='1'>");
			if(pageSize!=null){
				buffer.append("<input type='hidden' name='pageSize' id='pageSize' value='"+this.getPageSize()+"' />");
			}
			try {
				pageContext.getOut().print(buffer.toString()); 
				buffer.delete(0, buffer.length());
				pageResult=null;					
			} catch (Exception e) {
				log.error("分页控件出现异常"+e.getMessage());
			}
		}
		return EVAL_PAGE;
	}

	public int getMaxSize() {
		return maxSize;
	}

	public void setMaxSize(int maxSize) {
		this.maxSize = maxSize;
	}

	/**
	 * @return the pageSize
	 */
	public String getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	
}
